<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" />
<script type="text/javascript" src="common.js" />
<script>
var gTimer;
var gSabInfo;


$(document).ready(function() {
    setDefaults();
    startTimer();
});

function restartTimer() {
   startTimer(); 
}

function startTimer() {
    if(gTimer) {
        clearInterval(gTimer);
    } else {
        fetchInfo();
    }
    gTimer = setInterval(fetchInfo, getPref('refresh_rate')*1000);
    //gTimer = setInterval(fetchInfo, 5000);
}


function setDefaults() {
    if(!getPref('sab_url')) setPref('sab_url', 'http://localhost:8080/sabnzbd/');
    if(!getPref('api_key')) setPref('api_key', '');
    if(!getPref('speedlog')) setPref('speedlog', []);
    if(!getPref('show_graph')) setPref('show_graph', 0);
    if(!getPref('enable_newzbin')) setPref('enable_newzbin', 1);
    if(!getPref('enable_tvnzb')) setPref('enable_tvnzb', 1);
    if(!getPref('enable_nzbmatrix')) setPref('enable_nzbmatrix', 1);
    
    // Force this back to 0 just incase
    getPref('skip_redraw', 0);
    
    if(!getPref('refresh_rate')) setPref('refresh_rate', 15);
}


// Listen for notifications from the content script.
chrome.extension.onConnect.addListener(function(port, name) {
    port.onMessage.addListener(function(msg) {
        if(msg.get) {
            var value = getPref(msg.get);    
            sendConfigResponse(msg.get, value);
        }
    });
});


function sendConfigResponse(key, value) {
  chrome.tabs.getSelected(null, function(tab) {
    var port = chrome.tabs.connect(tab.id, {name: "notifyChannelOut"});
    port.postMessage({key: key, value: value});
  });
}


</script>
</head>
<body></body>
</html>